package server1;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author DIVID
 */
public class TCPEchoServer {

    public static int defaultPort = 2014;
    public static String nextServer = "";
    public static int nextServerPort = 0;

    public TCPEchoServer() {
        startServer();

    }

    TCPEchoServer(int port, String nextServer, int nextServerPort) {
        TCPEchoServer.defaultPort = port;
        TCPEchoServer.nextServer = nextServer;
        TCPEchoServer.nextServerPort = nextServerPort;
        startServer();
    }

    public static void startServer() {
        try {
            ServerSocket ss = new ServerSocket(defaultPort);
            System.out.println("Server dang hoat dong tai cong: " + defaultPort);
            while (true) {
                try {
                    Process p = new Process(ss.accept(), nextServer, nextServerPort);
                    p.start();
                } catch (IOException ex) {
                    Logger.getLogger(TCPEchoServer.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(TCPEchoServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

class Process extends Thread {

    Socket s;
    String nextServer = "";
    int nextServerPort = 0;
    String send = "@$01|100|141|Server1|Lock|1|74$$update chi set thienchi=5000000 where chid='6'@$";

    public Process() {
    }

    public Process(Socket s, String nextServer, int nextServerPort) {
        this.s = s;
        this.nextServer = nextServer;
        this.nextServerPort = nextServerPort;
    }

    @Override
    public synchronized void start() {
        try {
            PrintWriter out
                    = new PrintWriter(s.getOutputStream(), true);
            BufferedReader in
                    = new BufferedReader(
                            new InputStreamReader(s.getInputStream()));
            String str = in.readLine();
            str = str.substring(2, str.length() - 2);
            StringTokenizer token = new StringTokenizer(str, "$$");
            StringTokenizer tokenInfo = new StringTokenizer(token.nextToken(), "|");
            while (tokenInfo.hasMoreTokens()) {
                System.out.println(tokenInfo.nextToken());
            }
            String Query = token.nextToken();
            System.out.println(Query);
            s.close();
            send();
        } catch (IOException e) {
            System.out.println("ERROR: " + e);
        }
    }

    public void send() {
        try {
            Socket s = new Socket(nextServer, nextServerPort);
            PrintWriter out
                    = new PrintWriter(s.getOutputStream(), true);
            out.write(send);
            out.close();
            s.close();
        } catch (IOException ex) {
            Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}
